﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using ATA.Entities;
using ATA.Data;
using ATA.Data.Bases;
using ATA.Business;
#endregion

namespace ATA.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.ChietKhauSanPhamProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(ChietKhauSanPhamDataSourceDesigner))]
	public class ChietKhauSanPhamDataSource : ProviderDataSource<ChietKhauSanPham, ChietKhauSanPhamKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamDataSource class.
		/// </summary>
		public ChietKhauSanPhamDataSource() : base(new ChietKhauSanPhamService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the ChietKhauSanPhamDataSourceView used by the ChietKhauSanPhamDataSource.
		/// </summary>
		protected ChietKhauSanPhamDataSourceView ChietKhauSanPhamView
		{
			get { return ( View as ChietKhauSanPhamDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the ChietKhauSanPhamDataSource control invokes to retrieve data.
		/// </summary>
		public ChietKhauSanPhamSelectMethod SelectMethod
		{
			get
			{
				ChietKhauSanPhamSelectMethod selectMethod = ChietKhauSanPhamSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (ChietKhauSanPhamSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the ChietKhauSanPhamDataSourceView class that is to be
		/// used by the ChietKhauSanPhamDataSource.
		/// </summary>
		/// <returns>An instance of the ChietKhauSanPhamDataSourceView class.</returns>
		protected override BaseDataSourceView<ChietKhauSanPham, ChietKhauSanPhamKey> GetNewDataSourceView()
		{
			return new ChietKhauSanPhamDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the ChietKhauSanPhamDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class ChietKhauSanPhamDataSourceView : ProviderDataSourceView<ChietKhauSanPham, ChietKhauSanPhamKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the ChietKhauSanPhamDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public ChietKhauSanPhamDataSourceView(ChietKhauSanPhamDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal ChietKhauSanPhamDataSource ChietKhauSanPhamOwner
		{
			get { return Owner as ChietKhauSanPhamDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal ChietKhauSanPhamSelectMethod SelectMethod
		{
			get { return ChietKhauSanPhamOwner.SelectMethod; }
			set { ChietKhauSanPhamOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal ChietKhauSanPhamService ChietKhauSanPhamProvider
		{
			get { return Provider as ChietKhauSanPhamService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<ChietKhauSanPham> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<ChietKhauSanPham> results = null;
			ChietKhauSanPham item;
			count = 0;
			
			System.Guid chietKhauSpid;
			System.Guid nhomSanPhamId;
			System.Guid chietKhauId;
			System.Byte donViTinhId;

			switch ( SelectMethod )
			{
				case ChietKhauSanPhamSelectMethod.Get:
					ChietKhauSanPhamKey entityKey  = new ChietKhauSanPhamKey();
					entityKey.Load(values);
					item = ChietKhauSanPhamProvider.Get(entityKey);
					results = new TList<ChietKhauSanPham>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case ChietKhauSanPhamSelectMethod.GetAll:
                    results = ChietKhauSanPhamProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case ChietKhauSanPhamSelectMethod.GetPaged:
					results = ChietKhauSanPhamProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case ChietKhauSanPhamSelectMethod.Find:
					if ( FilterParameters != null )
						results = ChietKhauSanPhamProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = ChietKhauSanPhamProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case ChietKhauSanPhamSelectMethod.GetByChietKhauSpid:
					chietKhauSpid = ( values["ChietKhauSpid"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["ChietKhauSpid"], typeof(System.Guid)) : Guid.Empty;
					item = ChietKhauSanPhamProvider.GetByChietKhauSpid(chietKhauSpid);
					results = new TList<ChietKhauSanPham>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				case ChietKhauSanPhamSelectMethod.GetByNhomSanPhamId:
					nhomSanPhamId = ( values["NhomSanPhamId"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["NhomSanPhamId"], typeof(System.Guid)) : Guid.Empty;
					results = ChietKhauSanPhamProvider.GetByNhomSanPhamId(nhomSanPhamId, this.StartIndex, this.PageSize, out count);
					break;
				case ChietKhauSanPhamSelectMethod.GetByChietKhauId:
					chietKhauId = ( values["ChietKhauId"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["ChietKhauId"], typeof(System.Guid)) : Guid.Empty;
					results = ChietKhauSanPhamProvider.GetByChietKhauId(chietKhauId, this.StartIndex, this.PageSize, out count);
					break;
				case ChietKhauSanPhamSelectMethod.GetByDonViTinhId:
					donViTinhId = ( values["DonViTinhId"] != null ) ? (System.Byte) EntityUtil.ChangeType(values["DonViTinhId"], typeof(System.Byte)) : (byte)0;
					results = ChietKhauSanPhamProvider.GetByDonViTinhId(donViTinhId, this.StartIndex, this.PageSize, out count);
					break;
				// M:M
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == ChietKhauSanPhamSelectMethod.Get || SelectMethod == ChietKhauSanPhamSelectMethod.GetByChietKhauSpid )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Sets the primary key values of the specified Entity object.
		/// </summary>
		/// <param name="entity">The Entity object to update.</param>
		protected override void SetEntityKeyValues(ChietKhauSanPham entity)
		{
			base.SetEntityKeyValues(entity);
			
			// make sure primary key column(s) have been set
			if ( entity.ChietKhauSpid == Guid.Empty )
				entity.ChietKhauSpid = Guid.NewGuid();
		}
		
		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				ChietKhauSanPham entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					ChietKhauSanPhamProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<ChietKhauSanPham> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			ChietKhauSanPhamProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region ChietKhauSanPhamDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the ChietKhauSanPhamDataSource class.
	/// </summary>
	public class ChietKhauSanPhamDataSourceDesigner : ProviderDataSourceDesigner<ChietKhauSanPham, ChietKhauSanPhamKey>
	{
		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamDataSourceDesigner class.
		/// </summary>
		public ChietKhauSanPhamDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public ChietKhauSanPhamSelectMethod SelectMethod
		{
			get { return ((ChietKhauSanPhamDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new ChietKhauSanPhamDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region ChietKhauSanPhamDataSourceActionList

	/// <summary>
	/// Supports the ChietKhauSanPhamDataSourceDesigner class.
	/// </summary>
	internal class ChietKhauSanPhamDataSourceActionList : DesignerActionList
	{
		private ChietKhauSanPhamDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public ChietKhauSanPhamDataSourceActionList(ChietKhauSanPhamDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public ChietKhauSanPhamSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion ChietKhauSanPhamDataSourceActionList
	
	#endregion ChietKhauSanPhamDataSourceDesigner
	
	#region ChietKhauSanPhamSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the ChietKhauSanPhamDataSource.SelectMethod property.
	/// </summary>
	public enum ChietKhauSanPhamSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByChietKhauSpid method.
		/// </summary>
		GetByChietKhauSpid,
		/// <summary>
		/// Represents the GetByNhomSanPhamId method.
		/// </summary>
		GetByNhomSanPhamId,
		/// <summary>
		/// Represents the GetByChietKhauId method.
		/// </summary>
		GetByChietKhauId,
		/// <summary>
		/// Represents the GetByDonViTinhId method.
		/// </summary>
		GetByDonViTinhId
	}
	
	#endregion ChietKhauSanPhamSelectMethod

	#region ChietKhauSanPhamFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauSanPham"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauSanPhamFilter : SqlFilter<ChietKhauSanPhamColumn>
	{
	}
	
	#endregion ChietKhauSanPhamFilter

	#region ChietKhauSanPhamExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauSanPham"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauSanPhamExpressionBuilder : SqlExpressionBuilder<ChietKhauSanPhamColumn>
	{
	}
	
	#endregion ChietKhauSanPhamExpressionBuilder	

	#region ChietKhauSanPhamProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;ChietKhauSanPhamChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauSanPham"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauSanPhamProperty : ChildEntityProperty<ChietKhauSanPhamChildEntityTypes>
	{
	}
	
	#endregion ChietKhauSanPhamProperty
}

